﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

namespace Storage.EntityModels
{
    using Exceptions;

    #region Message
    
    public partial class Message
    {
        /// <summary>
        /// Create a new Message and the relatives entry in SentMessages and ReceivedMessages.
        /// </summary>
        /// <param name="title">The message's title.</param>
        /// <param name="text">The message's text.</param>
        /// <param name="senderID">The sender's id.</param>
        /// <param name="receiversUsernames">The list of receivers's usernames.</param>
        /// <param name="priority">True if the message has high priority.</param>
        /// <param name="trip">The related Trip.</param>
        /// <param name="message_event">The related Event.</param>
        /// <rereturns>A list that contains the non existing receivers.</rereturns>
        public static List<string> CreateMessage(string title, string text, int senderID, List<string> receiversUsernames,
                            bool priority, Trip trip, Event message_event)
        {
            Message msg = new Message()
            {
                Date = DateTime.Now,
                Event = message_event,
                Priority = priority,
                Text = text,
                Trip = trip,
                Title = title,
                Type = 0
            };

            var db = StorageManager.Instance.Entities;
            db.Messages.InsertOnSubmit(msg);

            User sender = StorageManager.Instance.GetUserById(senderID);

            List<string> not_existing_receivers = new List<string>();

            User receiver = null;
            SentMessages s_msg = null;
            ReceivedMessages r_msg = null;

            foreach (string username in receiversUsernames)
            {
                try
                {
                    receiver = StorageManager.Instance.GetUserByUserName(username);

                    s_msg = new SentMessages()
                    {
                        Message = msg,
                        Sender = sender,
                        Receiver = receiver,
                    };

                    r_msg = new ReceivedMessages()
                    {
                        Message = msg,
                        Sender = sender,
                        Receiver = receiver,
                        IsRead = false
                    };

                    db.SentMessages.InsertOnSubmit(s_msg);
                    db.ReceivedMessages.InsertOnSubmit(r_msg);
                }
                catch (UserNotFoundException)
                {
                    not_existing_receivers.Add(username);
                }
            }

            db.SubmitChanges();

            return not_existing_receivers;
        }
    } 

    #endregion

    #region SentMessages

    public partial class SentMessages
    {
#if DEBUG
        partial void OnValidate(ChangeAction action)
        {
            if (Sender == null)
                throw new MessageValidationException();
        }
#endif
    }

    #endregion

    #region ReceivedMessages

    public partial class ReceivedMessages
    {
#if DEBUG
        partial void OnValidate(ChangeAction action)
        {
            if (Sender == null)
                throw new MessageValidationException();
        }
#endif
    }

    #endregion
}
